package com.erpoa.controller;

import com.erpoa.annotation.DataBase;
import com.erpoa.constants.DataSourceNames;
import com.erpoa.mapper.CarDataMapper;
import com.erpoa.utils.DateUtils;
import com.erpoa.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


@RestController
@RequestMapping(value = "rest/redis/util")
@Api(value = "RedisUtilTestController", tags = {"redisUtil 工具测试"})
public class RedisUtilTestController {

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    private CarDataMapper carDataMapper;

    @RequestMapping(value = "v1/redisSave", method = {RequestMethod.GET})
    @ApiOperation(value = "保存到redis,并返回结果", notes = "保存到redis")
    public Object redisSave(@ApiParam(value = "传入key值") @RequestParam String key,
                            @ApiParam(value = "传入value值") @RequestParam String value) {
        for (int i = 0; i <= 544554; i++) {
            redisUtil.set(i + "hj", i + "jh", 3600);
            System.out.println(i + "key" + DateUtils.getTimeSSS());
        }

        redisUtil.set(key, value, 3600);
        return redisUtil.get(key);
    }

    @RequestMapping(value = "v1/redisGetAllKeyValue", method = {RequestMethod.GET})
    @ApiOperation(value = "获取redis中所有key和value值", notes = "获取redis中所有key和value值")
    @DataBase(DataSourceNames.WMS)
    @Transactional()
    public Object redisGetAllKeyValue() {


        boolean b = redisUtil.sHasKey("21010251982800070details", "P297310083210102D15654");

        if (redisUtil.hasKey("21010251982800070details" + "details")) {
            Set<Object> set1 = redisUtil.sGet("20123181982800094" + "details");
            for (Object set2 : set1) {
                System.out.println("details----------------------" + set2);
                try {
                    int des = this.carDataMapper.updateSqlDetailredis("20123081994270247", set2.toString().split("/")[0], set2.toString().split("/")[1]);
                    System.out.println("redisdetail补偿" + des + "个");
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }


        Set<String> keys = redisUtil.keys("*details");
        Iterator<String> iterator = keys.iterator();
        Map<String, Object> map = new HashMap<>();
        while (iterator.hasNext()) {
            String key = iterator.next();
            Object o = redisUtil.get(key);
            System.out.println(key + o + "/////" + DateUtils.getTimeSSS());
            map.put(key, o);
        }
        return map;
    }


    @RequestMapping(value = "v1/deleteRedisAll", method = {RequestMethod.DELETE})
    @ApiOperation(value = "清除redis所有缓存", notes = "清除redis所有缓存")
    public Object deleteRedisAll() {
        Set<String> keys = redisUtil.keys("*");
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()) {
            redisUtil.del(iterator.next());
        }
        return "删除redis所有数据成功";
    }

    @RequestMapping(value = "v1/getRedisValue", method = {RequestMethod.GET})
    @ApiOperation(value = "根据key获取value值", notes = "根据key获取value值")
    public Object getRedisValue(@ApiParam(value = "传入key值") @RequestParam String key) {
        return redisUtil.get(key);
    }

    @RequestMapping(value = "v1/incrValue", method = {RequestMethod.GET})
    @ApiOperation(value = "递增因子必须大于0", notes = "递增因子必须大于0")
    public long getRedisValue(@ApiParam(value = "传入key值") @RequestParam String key,
                              @ApiParam(value = "递增因子必须大于0") @RequestParam String delta) {

        return redisUtil.incr(key, Long.valueOf(delta));
    }




}
